package com.nhim.cms.admin.client.eldf.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.nhim.cms.admin.shared.ElementTemplateProxy;

public class ElementDefinitionTemplate extends PopupPanel {
	Logger logger = Logger
			.getLogger(ElementDefinitionTemplate.class.toString());
	private static ElementDefinitionTemplateUiBinder uiBinder = GWT
			.create(ElementDefinitionTemplateUiBinder.class);

	@UiTemplate("ElementDefinitionTemplate.ui.xml")
	interface ElementDefinitionTemplateUiBinder extends
			UiBinder<Widget, ElementDefinitionTemplate> {
	}

	@UiField
	TextBox txtName;
	@UiField
	TextArea txtAHeader;
	@UiField
	TextArea txtABody;
	@UiField
	TextArea txtAFooter;
	@UiField
	TextArea txtAEmpty;
	@UiField
	Button btnAdd;
	@UiField
	Button btnCancel;
	private Boolean isNew = false;

	public ElementDefinitionTemplate() {
		super(false, true);
		add(uiBinder.createAndBindUi(this));
		this.txtABody.setVisibleLines(5);
		this.txtAFooter.setVisibleLines(5);
		this.txtAHeader.setVisibleLines(5);
		this.txtAEmpty.setVisibleLines(5);
	}

	private void clearDisplay() {
		this.txtName.setText("");
		this.txtABody.setText("");
		this.txtAFooter.setText("");
		this.txtAHeader.setText("");
		this.txtAEmpty.setText("");
	}

	public void showTemplatePopup(ElementTemplateProxy elementTemplate, Boolean isNew) {
		clearDisplay();
		this.isNew = isNew;
		this.txtName.setEnabled(true);
		if (!isNew) {
			if ("default".equals(elementTemplate.getName().toLowerCase()))
				this.txtName.setEnabled(false);
			this.txtName.setText(elementTemplate.getName());
			this.txtABody.setText(elementTemplate.getNormalText());
			this.txtAFooter.setText(elementTemplate.getFooterText());
			this.txtAHeader.setText(elementTemplate.getHeaderText());
			this.txtAEmpty.setText(elementTemplate.getEmptyText());
		}
	}

	@UiHandler("btnCancel")
	void handleButtonCancelClick(ClickEvent e) {
		this.hide();

	}

	public HasClickHandlers getAddButton() {
		return btnAdd;

	}
	public Boolean isNew()
	{
		return this.isNew;
	}
	public String getName()
	{
		return this.txtName.getText();
	}

	public List<String> validate() {
		List<String> validation = new ArrayList<String>();
		if(this.txtName.getText().isEmpty())
			validation.add("The Template name can not be empty.");
		return validation;
	}

	public void save(ElementTemplateProxy elementTemplate) {
		try {

			elementTemplate.setName(this.txtName.getText());
			elementTemplate.setEmptyText(this.txtAEmpty.getText());
			elementTemplate.setFooterText(this.txtAFooter.getText());
			elementTemplate.setHeaderText(this.txtAHeader.getText());
			elementTemplate.setNormalText(this.txtABody.getText());
		} catch (Exception ex) {
			logger.log(Level.SEVERE, ex.getMessage());
		}
	}

}
